<template>
  <!-- 添加对话框 -->
  <el-dialog
    :title="title"
    :visible.sync="open"
    :before-close="cancel"
    width="800px"
    append-to-body
  >
    <el-form ref="form" :model="form" :rules="rules" label-width="120px">
      <el-row>
        <el-col :span="12">
          <el-form-item label="项目编号" prop="code">
            <el-input v-model="form.code" placeholder="请输入项目编号" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="项目名称" prop="name">
            <el-input v-model="form.name" placeholder="请输入项目名称" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="项目类别" prop="category">
            <el-select v-model="form.category" placeholder="请选择项目名称" clearable>
              <el-option
                v-for="dict in dict.type.items_type"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="项目类型" prop="type">
            <el-select v-model="form.type" placeholder="请选择项目名称" clearable>
              <el-option
                v-for="dict in dict.type.items_category"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="开工时间" prop="startTime">
            <el-date-picker
              v-model="form.startTime"
              type="datetime"
              placeholder="请选择开工时间"
            >
            </el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="竣工时间" prop="endTime">
            <el-date-picker
              v-model="form.endTime"
              type="datetime"
              placeholder="请选择竣工时间"
            >
            </el-date-picker>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="项目状态" prop="status">
            <!-- <el-input v-model="form.status" placeholder="请输入开工时间" /> -->
            <el-select v-model="form.status" placeholder="请选择项目状态" clearable>
              <el-option
                v-for="dict in dict.type.items_status"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="经理名称" prop="manager">
            <el-input v-model="form.manager" placeholder="请输入经理名称" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="合同金额" prop="amount">
            <el-input-number
              v-model="form.amount"
              controls-position="right"
              @change="handleChange"
            ></el-input-number>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="甲方单位" prop="partyA">
            <el-input v-model="form.partyA" placeholder="请输入甲方单位" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="项目地址" prop="address">
            <el-input v-model="form.address" placeholder="请输入项目地址" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="建筑面积(㎡)" prop="floorArea">
            <el-input-number
              v-model="form.floorArea"
              controls-position="right"
              @change="handlefloorArea"
            ></el-input-number>
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item label="项目概况" prop="proDesc">
        <el-input v-model="form.proDesc" type="textarea" placeholder="请输入内容" />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { addProject, updateProject } from "@/api/tms/project";
import moment from "moment";
export default {
  name: "AddProject",
  dicts: ["items_category", "items_type", "items_status"],
  props: {
    open: {
      type: Boolean,
    },
    title: String,
  },
  data() {
    return {
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [{ required: true, message: "项目名称不能为空", trigger: "blur" }],
        address: [{ required: true, message: "项目地址不能为空", trigger: "blur" }],
        startTime: [{ required: true, message: "开工时间不能为空", trigger: "blur" }],
        endTime: [{ required: true, message: "竣工时间不能为空", trigger: "blur" }],
        amount: [{ required: true, message: "合同金额不能为空", trigger: "blur" }],
        floorArea: [{ required: true, message: "建筑面积(㎡)不能为空", trigger: "blur" }],
      },
    };
  },
  methods: {
    handleChange(value) {
      this.form.amount = value;
    },
    handlefloorArea(value) {
      this.form.floorArea = value;
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          this.form.type = Number(this.form.type);
          this.form.category = Number(this.form.category);
          this.form.status = Number(this.form.status);
          this.form.amount = Number(this.form.amount);
          this.form.floorArea = Number(this.form.floorArea);
          this.form.startTime = moment(this.form.startTime).format("YYYY-MM-DD HH:mm:ss");
          this.form.endTime = moment(this.form.endTime).format("YYYY-MM-DD HH:mm:ss");
          console.log(this.form);
          addProject(this.form).then((response) => {
            console.log(response);
            this.$modal.msgSuccess("新增成功");
            this.$emit("close");
          });
        }
      });
    },
    // 取消按钮
    cancel() {
      this.reset();
      this.$emit("close");
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        agencyId: null,
        code: null,
        name: null,
        type: null,
        category: null,
        province: null,
        city: null,
        address: null,
        startTime: null,
        endTime: null,
        status: 0,
        manager: null,
        amount: null,
        partyA: null,
        floorArea: null,
        proDesc: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        deleted: null,
      };
      this.resetForm("form");
    },
  },
};
</script>

<style></style>
